package com.ruoyi.system.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.system.domain.SysDomainName;
import com.ruoyi.system.mapper.SysDomainNameMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysExamine;
import com.ruoyi.system.service.ISysExamineService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 检测配置Controller
 *
 * @author ruoyi
 * @date 2024-11-30
 */
@RestController
@RequestMapping("/system/examine")
public class SysExamineController extends BaseController
{
    @Autowired
    private ISysExamineService sysExamineService;

    @Resource
    private SysDomainNameMapper sysDomainNameMapper;

    /**
     * 查询检测配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:examine:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysExamine sysExamine)
    {
        startPage();
        List<SysExamine> list = sysExamineService.selectSysExamineList(sysExamine);
        return getDataTable(list);
    }

    /**
     * 导出检测配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:examine:export')")
    @Log(title = "检测配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysExamine sysExamine)
    {
        List<SysExamine> list = sysExamineService.selectSysExamineList(sysExamine);
        ExcelUtil<SysExamine> util = new ExcelUtil<SysExamine>(SysExamine.class);
        util.exportExcel(response, list, "检测配置数据");
    }

    /**
     * 获取检测配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:examine:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysExamineService.getById(id));
    }

    /**
     * 新增检测配置
     */
    @PreAuthorize("@ss.hasPermi('system:examine:add')")
    @Log(title = "检测配置", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody SysExamine sysExamine) {
        if(sysExamine.getThreshold() != 0 && Objects.nonNull(sysExamine.getThreshold())) {
            sysDomainNameMapper.updates(sysExamine.getThreshold());
        }
        return toAjax(sysExamineService.save(sysExamine));
    }

    /**
     * 修改检测配置
     */
    @PreAuthorize("@ss.hasPermi('system:examine:edit')")
    @Log(title = "检测配置", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody SysExamine sysExamine) {
        if(sysExamine.getThreshold() != 0 && Objects.nonNull(sysExamine.getThreshold())) {
            sysDomainNameMapper.updates(sysExamine.getThreshold());
        }
        return toAjax(sysExamineService.updateById(sysExamine));
    }

    /**
     * 删除检测配置
     */
    @PreAuthorize("@ss.hasPermi('system:examine:remove')")
    @Log(title = "检测配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysExamineService.removeByIds(Arrays.asList(ids)));
    }
}
